//
//  ZZCSetUpController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCSetUpController.h"
#import "ZZCLoginWayController.h"
#import "YHActionSheet.h"
#import "ZZCAlterPasswordController.h"
#import "ZZCIdeaFeedbackController.h"
#import "ZZCPayPasswordController.h"
#import "ZZCSetPayPwdOneController.h"
#import "ZZCAboutOurController.h"


@interface ZZCSetUpController ()
//修改密码
@property (weak, nonatomic) IBOutlet UIView *alterPasswordView;
//支付密码
@property (weak, nonatomic) IBOutlet UIView *payPasswordView;
//登录方式
@property (weak, nonatomic) IBOutlet UIView *loginWayView;
//关于我们
@property (weak, nonatomic) IBOutlet UIView *aboutOurView;
//意见反馈
@property (weak, nonatomic) IBOutlet UIView *ideaFeedbackView;
//清除缓存
@property (weak, nonatomic) IBOutlet UIView *clearCacheView;
//退出登录
@property (weak, nonatomic) IBOutlet UIView *signOutView;
//缓存
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;

@property (weak, nonatomic) IBOutlet UIView *onLoginView;

@end

@implementation ZZCSetUpController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    IQKeyboardManager *iq = [IQKeyboardManager sharedManager];
    iq.enableAutoToolbar = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
        self.title = @"设置";
//    if (self.isLogin) {
        [super viewDidLoad];
        self.view.backgroundColor = ZZCBGColor;
        self.onLoginView.hidden = YES;
        [self addAGestureToView];
    float chacha = [[NSUserDefaults standardUserDefaults] floatForKey:@"appchacha"];
    [[NSUserDefaults standardUserDefaults] setFloat:chacha forKey:@"appchacha"];
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fMB",[[NSUserDefaults standardUserDefaults] floatForKey:@"appchacha"]];

//    }else {
//        self.onLoginView.hidden = NO;
//    }
}

//登录点击
- (IBAction)loginClick {
    ZZCLoginController *login = [ZZCLoginController new];
    [self.navigationController pushViewController:login animated:YES];
}
//给view添加手势
- (void)addAGestureToView {
   
    //修改密码
     self.alterPasswordView.userInteractionEnabled = YES;
    UITapGestureRecognizer *alterTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alterTapClick)];
    [self.alterPasswordView addGestureRecognizer:alterTap];
    //支付密码
     self.payPasswordView.userInteractionEnabled = YES;
    UITapGestureRecognizer *payTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(payTapClick)];
    [self.payPasswordView addGestureRecognizer:payTap];
    //登录方式
     self.loginWayView.userInteractionEnabled = YES;
    UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginTapClick)];
    [self.loginWayView addGestureRecognizer:loginTap];
    //关于我们
     self.aboutOurView.userInteractionEnabled = YES;
    UITapGestureRecognizer *aboutTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aboutTapClick)];
    [self.aboutOurView addGestureRecognizer:aboutTap];
    //意见反馈
     self.ideaFeedbackView.userInteractionEnabled = YES;
    UITapGestureRecognizer *ideaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ideaTapClick)];
    [self.ideaFeedbackView addGestureRecognizer:ideaTap];
    //清除缓存
    self.clearCacheView.userInteractionEnabled = YES;
    UITapGestureRecognizer *clearTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearTapClick)];
    [self.clearCacheView addGestureRecognizer:clearTap];
    //退出登录
     self.signOutView.userInteractionEnabled = YES;
    UITapGestureRecognizer *signTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(signTapClick)];
    [self.signOutView addGestureRecognizer:signTap];
}

#pragma mark - 监听点击事件
//修改密码
- (void)alterTapClick {
    ZZCAlterPasswordController *alter = [ZZCAlterPasswordController new];
    [self.navigationController pushViewController:alter animated:YES];
}
//支付密码
- (void)payTapClick{
    BOOL is_set_paypassward = [[NSUserDefaults standardUserDefaults] boolForKey:@"is_set_paypassward"];
    NSLog(@"%d",is_set_paypassward);
    if (is_set_paypassward) {
        ZZCPayPasswordController *Pay = [ZZCPayPasswordController new];
        [self.navigationController pushViewController:Pay animated:YES];
    } else {
        ZZCSetPayPwdOneController *setpay = [ZZCSetPayPwdOneController new];
        [self.navigationController pushViewController:setpay animated:YES];
    }

}
//登录方式
- (void)loginTapClick{
    ZZCLoginWayController *login = [ZZCLoginWayController new];
    [self.navigationController pushViewController:login animated:YES];
}
//关于我们
- (void)aboutTapClick{
    ZZCAboutOurController *About = [ZZCAboutOurController new];
    [self.navigationController pushViewController:About animated:YES];
}

//意见反馈
- (void)ideaTapClick{
    ZZCIdeaFeedbackController *Idea = [ZZCIdeaFeedbackController new];
    [self.navigationController pushViewController:Idea animated:YES];
}
//清除缓存
- (void)clearTapClick{
    YHActionSheet *aSheet = [[YHActionSheet alloc] initWithCancelTitle:@"取消" otherTitles:@[@"清理内存"]];
    [aSheet dismissForCompletionHandle:^(NSInteger clickedIndex, BOOL isCancel) {
        if (!isCancel) {
            switch (clickedIndex) {
                case 0:
                    ZZCLog(@"清理内存");
                    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"appchacha"];
                    NSLog(@"%f",[[NSUserDefaults standardUserDefaults] floatForKey:@"appchacha"]);
                    self.cacheLabel.text = @"0.00MB";
                    break;
                default:
                    break;
            }
        }
    }];
    [aSheet show];

}
//退出登录
- (void)signTapClick{
    YHActionSheet *aSheet = [[YHActionSheet alloc] initWithCancelTitle:@"取消" otherTitles:@[@"退出登录"]];
    [aSheet dismissForCompletionHandle:^(NSInteger clickedIndex, BOOL isCancel) {
        if (!isCancel) {
            switch (clickedIndex) {
                case 0:
                    ZZCLog(@"退出登录");
                    [self logout];
                    break;
                default:
                    break;
            }
        }
    }];
    [aSheet show];
}

- (void)logout {
    NSString *userphone = ZZCNSUserDefaults(@"userphone");
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_LOGOUT parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginLeave" object:self];
            NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
            NSDictionary *dictionary = [defatluts dictionaryRepresentation];
            for(NSString *key in [dictionary allKeys]){
                [defatluts removeObjectForKey:key];
                [defatluts synchronize];
            }
            [[NSUserDefaults standardUserDefaults] setObject:userphone forKey:@"userphone"];
        }
    }];
}
//返回
- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
